package com.huike.controller;

import com.huike.po.Friend;
import com.huike.po.FriendShip;
import com.huike.pojo.FriendCustomBean;
import com.huike.pojo.Response;
import com.huike.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
/**
 * 好友相关操作的请求处理
 */
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private Response response;

    @RequestMapping(value = "/addSubscribe")
    @ResponseBody
    /**
     * 添加好友在线订阅
     */
    public Response addSubscribe(String account, String friendAccount ,HttpSession session) {
        if (null == session.getAttribute("loginUser")) {
            response.setCode("400");
            response.setContent("请先登录");
            return response;
        }
        if(friendService.handleSubscribe(account, friendAccount, "addSubscribe"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/cancelSubscribe")
    @ResponseBody
    /**
     * 取消好友在线订阅
     */
    public Response cancelSubscribe(String account, String friendAccount ) {
        if(friendService.handleSubscribe(account, friendAccount, "cancelSubscribe"))
            response.setCode("200");
        else
            response.setCode("500");
        return response;
    }


    @RequestMapping(value = "/applyFriend")
    @ResponseBody
    /**
     * 好友添加申请
     */
    public Response applyFriend(@RequestBody FriendCustomBean friendRequest) {

        if (friendService.handleFriend(friendRequest,"applyFriend"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/agreeApplyFriend")
    @ResponseBody
    /**
     * 好友申请通过
     */
    public Response agreeApplyFriend(@RequestBody FriendCustomBean friendRequest) {
        if (friendService.handleFriend(friendRequest,"agreeApplyFriend")){
            //A同意B的好友申请，A前端实时更新好友记录
            Friend friend = friendService.searchFriendRelation(friendRequest.getAccountFrom(), friendRequest.getAccountTo());
            response.setCode("200");
            response.setContent(friend);
        }
        else{
            response.setCode("500");
            response.setContent("");
        }
        return response;
    }

    @RequestMapping(value = "/refuseApplyFriend")
    @ResponseBody
    /**
     * 拒绝好友申请
     */
    public Response refuseApplyFriend(@RequestBody FriendCustomBean friendRequest) {
        if (friendService.handleFriend(friendRequest,"refuseApplyFriend"))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/alias",method = RequestMethod.POST)
    @ResponseBody
    /**
     * 修改好友备注
     */
    public Response updateAlias(@RequestBody FriendShip friend) {
        if(friendService.updateFriendAlias(friend)){
            response.setCode("200");
        }else {
            response.setCode("500");
        }
        return response;
    }

    @RequestMapping(value = "/tags",method = RequestMethod.POST)
    @ResponseBody
    /**
     * 修改好友标签
     */
    public Response updateTags(@RequestBody FriendShip friend) {
        if(friendService.updateFriendTags(friend)){
            response.setCode("200");
        }else {
            response.setCode("500");
        }
        return response;
    }

    @RequestMapping(value = "/deleteFriend")
    @ResponseBody
    /**
     * 好友删除
     */
    public Response deleteFriend(String account, String friendAccount) {
        if(friendService.deleteFriend(account,friendAccount))
            response.setCode("200");
        else
            response.setCode("500");
       response.setContent("");

        return response;
    }

    @RequestMapping(value = "/setBlacked",method = RequestMethod.POST)
    @ResponseBody
    /**
     * 好友拉黑
     */
    public Response setFriendBlacked(@RequestBody FriendShip friend) {
        if(friendService.setFriendBlacked(friend)){
            response.setCode("200");
        }else {
            response.setCode("500");
        }
        return response;
    }

    @RequestMapping(value = "/cancelBlacked",method = RequestMethod.POST)
    @ResponseBody
    /**
     * 取消好友拉黑
     */
    public Response cancelFriendBlacked(@RequestBody FriendShip friend) {
        if(friendService.cancelFriendBlacked(friend)){
            response.setCode("200");
        }else {
            response.setCode("500");
        }
        return response;
    }
}
